﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Data;

namespace BeerManagementSystem
{
    /// <summary>
    /// Interaktionslogik für MonthlyReport.xaml
    /// </summary>
    public partial class MonthlyReport : Window
    {
        private LinqToBeerDataContext _data;

        public MonthlyReport()
        {
            InitializeComponent();

            _data = new LinqToBeerDataContext();

            monthTextBox.Text = DateTime.Today.Month.ToString();
            yearTextBox.Text = DateTime.Today.Year.ToString();

            createReport();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            createReport();
        }

        private void createReport()
        {
            int month = 0;
            int year = 0;

            try
            {
                month = Convert.ToInt32(monthTextBox.Text);
                year = Convert.ToInt32(yearTextBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehlerhafte Eingabe für Monat/Jahr.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            DateTime reportStart;
            DateTime reportEnd;

            reportStart = new DateTime(year, month, 1, 0, 0, 0);
            reportEnd = reportStart.AddMonths(1);

            Debug.WriteLine("reportStart: " + reportStart.ToShortDateString());
            Debug.WriteLine("reportEnd: " + reportEnd.ToShortDateString());

            DataTable table = new DataTable();
            int total, totalDrunk, wineTotal, shotTotal;
            double costs;
            decimal account_Other;

            table.Columns.Add("Vorname", typeof(string));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Bier Total", typeof(int));
            table.Columns.Add("Bier Dokter", typeof(float));
            table.Columns.Add("Wein Total", typeof(float));
            table.Columns.Add("Shot Total", typeof(float));
            table.Columns.Add("Kosten", typeof(float));
            table.Columns.Add("Account_Other", typeof(float));

            foreach (User u in _data.User)
            {
                string firstName;
                string name;
                


                firstName = u.FirstName;
                name = u.LastName;

                var transactions =
                    from t in u.Transaction
                    where ((t.Timestamp.CompareTo(reportStart) > 0) && (t.Timestamp.CompareTo(reportEnd) < 0) && (t.Type != 2) &&(t.Drink_Type == 1))
                    select t;
               
                var transactionsDrunk =
                   from t in u.Transaction
                   where ((t.Timestamp.CompareTo(reportStart) > 0) && (t.Timestamp.CompareTo(reportEnd) < 0) && (t.Type != 3) && (t.Drink_Type == 1))
                   select t;

                var wineTransactions = 
                    from t in u.Transaction
                    where ((t.Timestamp.CompareTo(reportStart) > 0) && (t.Timestamp.CompareTo(reportEnd) < 0) && (t.Type != 2) && (t.Drink_Type == 2))
                    select t;

                var shotTransactions =
                    from t in u.Transaction
                    where ((t.Timestamp.CompareTo(reportStart) > 0) && (t.Timestamp.CompareTo(reportEnd) < 0) && (t.Type != 2) && (t.Drink_Type == 3))
                    select t;

                total = transactions.Count();

                if (total == 0)
                    continue;

                totalDrunk = transactionsDrunk.Count();
                wineTotal = wineTransactions.Count();
                shotTotal = shotTransactions.Count();
                costs = total * 0.75 + wineTotal * 1.5 + shotTotal * 0.5;
                account_Other = Convert.ToDecimal(u.Account_other);

                DataRow row = table.NewRow();

                row["Vorname"] = firstName;
                row["Name"] = name;
                row["Bier Total"] = total;
                row["Bier Dokter"] = totalDrunk;
                row["Wein Total"] = wineTotal;
                row["Shot Total"] = shotTotal;
                row["Kosten"] = costs;
                row["Account_other"] = account_Other;

                table.Rows.Add(row);
            }
            reportGrid.ItemsSource = table.DefaultView;
        }

        private void nextMonthButton_Click(object sender, RoutedEventArgs e)
        {
            changeMonth(1);
            createReport();
        }

        private void previousMonthButton_Click(object sender, RoutedEventArgs e)
        {
            changeMonth(-1);
            createReport();
        }
        private void deleteAccountOtherButton_Click(object sender, RoutedEventArgs e)
        {
            var User =
                   from u in _data.User
                   select u;
            foreach (User u in User)
            {
                u.Account_other = Convert.ToDecimal(0.0);
            }
        }

        private void changeMonth(int diff)
        {
            int month = 0;
            int year = 0;

            try
            {
                month = Convert.ToInt32(monthTextBox.Text);
                year = Convert.ToInt32(yearTextBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehlerhafte Eingabe für Monat/Jahr.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (diff > 0)
            {
                month++;

                if (month > 12)
                {
                    year++;
                    month = 1;
                }
            }
            else if (diff < 0)
            {
                month--;

                if (month < 1)
                {
                    year--;
                    month = 12;
                }
            }

            monthTextBox.Text = month.ToString();
            yearTextBox.Text = year.ToString();
        }

        
    }
}
